package com.ljtxxhwj.myblob.controller;

import com.ljtxxhwj.myblob.entity.FavoriteFolderHasFavorite;
import com.ljtxxhwj.myblob.service.FavoriteFolderHasFavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/favorite-folders/favorites.do")
public class FavoriteFolderHasFavoriteController {

    private final FavoriteFolderHasFavoriteService favoriteFolderHasFavoriteService;

    @Autowired
    public FavoriteFolderHasFavoriteController(FavoriteFolderHasFavoriteService favoriteFolderHasFavoriteService) {
        this.favoriteFolderHasFavoriteService = favoriteFolderHasFavoriteService;
    }

    // 插入新的收藏夹和收藏的关联关系
    @PostMapping("/{favoriteFolderId}/{favoriteId}.do")
    public ResponseEntity<String> addFavoriteFolderHasFavorite(
            @PathVariable Integer favoriteFolderId,
            @PathVariable Integer favoriteId) {
        FavoriteFolderHasFavorite relation = new FavoriteFolderHasFavorite(favoriteFolderId, favoriteId);
        favoriteFolderHasFavoriteService.addFavoriteFolderHasFavorite(relation);
        return new ResponseEntity<>("Relation added successfully", HttpStatus.CREATED);
    }

    // 根据收藏夹ID删除关联的收藏
    @DeleteMapping("/folder/{favoriteFolderId}.do")
    public ResponseEntity<String> removeFavoritesByFavoriteFolderId(@PathVariable Integer favoriteFolderId) {
        favoriteFolderHasFavoriteService.removeFavoritesByFavoriteFolderId(favoriteFolderId);
        return new ResponseEntity<>("Relations removed successfully", HttpStatus.OK);
    }

    // 根据收藏ID删除关联的收藏夹
    @DeleteMapping("/item/{favoriteId}.do")
    public ResponseEntity<String> removeFavoriteFoldersByFavoriteId(@PathVariable Integer favoriteId) {
        favoriteFolderHasFavoriteService.removeFavoriteFoldersByFavoriteId(favoriteId);
        return new ResponseEntity<>("Relations removed successfully", HttpStatus.OK);
    }

    // 根据收藏夹ID查询关联的收藏列表
    @GetMapping("/folder/{favoriteFolderId}.do")
    public List<Integer> getFavoriteIdsByFavoriteFolderId(@PathVariable Integer favoriteFolderId) {
        return favoriteFolderHasFavoriteService.getFavoriteIdsByFavoriteFolderId(favoriteFolderId);
    }

    // 根据收藏ID查询关联的收藏夹列表
    @GetMapping("/item/{favoriteId}.do")
    public List<Integer> getFavoriteFolderIdsByFavoriteId(@PathVariable Integer favoriteId) {
        return favoriteFolderHasFavoriteService.getFavoriteFolderIdsByFavoriteId(favoriteId);
    }
}